package com.wwwarehouse.usercenter.fragment.permissiondistribute;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.authoritydistribute.DistributeResultBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;


/**
 * 分配权限->权限分配成功
 * Created by ShineF on 2017/9/25 0025.
 */

public class PermissionDistributeSuccessFragment extends BaseFragment implements View.OnClickListener {


    private Button mBtnContinue = null;
    private Button mBtnCardDesk = null;
    private TextView mTvNotOnLineFriend = null;
    private StateLayout mStateLayout = null;
    private TextView mTvDistributeAnySuccess = null;
    private DistributeResultBean bean = null;
    private LinearLayout mCardItem = null;
    private String mBusinessUnitId;
    private String mType;
    private Bundle mBundle;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_distribute_success, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mBtnContinue = $(R.id.btn_continue_distribute);
        mBtnCardDesk = $(R.id.btn_goto_desk);
        mTvNotOnLineFriend = $(R.id.tv_not_on_line_friend);
        mTvDistributeAnySuccess = $(R.id.tv_distribute_info);
        mStateLayout = $(R.id.custom_state);
        mCardItem = $(R.id.ll_card);
        mCardItem.setVisibility(View.GONE);
        ((BaseCardDeskActivity) mActivity).hideBackBt();
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof PermissionDistributeSuccessFragment) {
            mActivity.setTitle(R.string.receiver_permission);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((BaseCardDeskActivity) mActivity).showBackBt();
    }

    @Override
    public void requestDatas() {
        mBundle = getArguments();
        if (null == mBundle) {
            return;
        }
        mType = mBundle.getString("type");
        bean = mBundle.getParcelable("resultBean");
        mBusinessUnitId = sp.getValue(UserCenterConstant.SAVE_BUSINESSUNITID);
        requestData();
        mBtnContinue.setOnClickListener(this);
        mBtnCardDesk.setOnClickListener(this);
        mTvNotOnLineFriend.setOnClickListener(this);
    }

    public void requestData() {
        showProgressDialog("正在加载...");
        Map<String, Object> map = new HashMap<>();
        map.put("processId", bean.getProcessId());
        loadData(UserCenterConstant.QUERY_NOT_ON_LINE_FIREND, map, 1);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_continue_distribute) {
            Bundle bundle = new Bundle();
            switch (mType) {
                case "distribution":
                    CardDeskFunctionResponseBean.TaskBean bean = new CardDeskFunctionResponseBean.TaskBean();
                    bean.setBusinessId(mBusinessUnitId);
                    bundle.putSerializable(Constant.KEY_CARD_DESK_FUNCTION_DETAILS, bean);
                    break;
                case "CheckDownClassFragment":
                    bundle.putString("personName", mBundle.getString("personName"));
                    bundle.putString("userId", mBundle.getString("userId"));
                    bundle.putString("buId", mBundle.getString("buId"));
                    bundle.putString("CheckDownClassFragment", "CheckDownClassFragment");
                    break;
            }
            AuthorityDistributePagerFragment fragment = new AuthorityDistributePagerFragment();
            fragment.setArguments(bundle);
            EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
            pushFragment(fragment);
        } else if (view.getId() == R.id.btn_goto_desk) {
            EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
        } else if (view.getId() == R.id.tv_not_on_line_friend) {
            EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
            pushFragment(new SelectNotOnlineFriendFragment());
        }
    }

    public void loadData(String url, Map map, int what) {
        NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int i) {
                if ("0".equals(commonClass.getCode())) {
                    String data = commonClass.getData().toString();
                    if (null != data) {
                        setData(data);
                    } else {
                        mStateLayout.showEmptyView("服务器抽风了", true);
                    }
                } else {
                    mStateLayout.showEmptyView(commonClass.getMsg(), true);
                }
            }

            @Override
            public void onFailed(String s, int i) {

            }

            @Override
            public void onFinish(int i) {
                setData("0");
            }
        }, what);
    }

    @SuppressLint("StringFormatMatches")
    public void setData(String data) {
        DistributeResultBean.DetailBean detail = bean.getDetail();
        int count = Integer.parseInt(data);
        if (count == 0) {
            mTvNotOnLineFriend.setVisibility(View.GONE);
            mTvDistributeAnySuccess.setText(String.format(getString(
                    R.string.distribute_any_success_no_on_line)
                    , detail.getAuthorityCount()
                    , detail.getSelectedUserCount()));
        } else {
            mTvNotOnLineFriend.setVisibility(View.INVISIBLE);
            mTvDistributeAnySuccess.setText(String.format(getString(
                    R.string.distribute_any_success)
                    , detail.getAuthorityCount()
                    , detail.getSelectedUserCount()
                    , data));
        }
        mCardItem.setVisibility(View.VISIBLE);
        mStateLayout.showContentView();
        dismissProgressDialog();
    }
}
